package cn.slipi.pan.web.controller;

import cn.hutool.core.util.StrUtil;
import cn.slipi.pan.common.base.BaseController;
import cn.slipi.pan.common.response.ApiResult;
import cn.slipi.pan.common.util.AESUtil;
import cn.slipi.pan.web.entity.RecordEntity;
import cn.slipi.pan.web.service.RecordService;
import lombok.extern.slf4j.Slf4j;
import org.noear.solon.annotation.*;

import java.util.Date;

@Slf4j
@Controller
@Mapping("/record")
public class RecordController extends BaseController {
    private static final String AUTH_KEY = "AB#SLIPI$LICENSE";

    @Inject
    private RecordService recordService;

    @Get
    @Mapping("/save")
    public ApiResult<?> save(@Param("info") String info) {
        //解密
        try {
            String exInfo = AESUtil.decrypt(info, AUTH_KEY, AUTH_KEY);
            if (StrUtil.isNotBlank(exInfo)) {
                //id + "|" + parent + "|" + data.data + "|" + pwd + "|" + type + "|" + filename + "|" + license
                String[] infoArr = exInfo.split("\\|");
                RecordEntity recordEntity = new RecordEntity().setRId(infoArr[0])
                        .setPId(infoArr[1]).setUrl(infoArr[2])
                        .setPwd(infoArr[3]).setType(infoArr[4])
                        .setFilename(infoArr[5]).setLicense(infoArr[6])
                        .setCreateTime(new Date());
                recordService.saveRecord(recordEntity);
            }
        } catch (Exception e) {
            log.error("解密失败:{}", info, e);
        }
        return success();
    }
}
